//
//  HEAddressInputCell.m
//  HealthFamily
//
//  Created by 吴欧 on 2020/11/12.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HEAddressInputCell.h"

@interface HEAddressInputCell ()<QMUITextFieldDelegate>

@property(nonatomic, strong) UILabel *titleLb;
@property(nonatomic, strong) QMUITextField *contentTf;
@property(nonatomic, assign) NSInteger row;
@property(nonatomic, strong) UITapGestureRecognizer *tap;

@property(nonatomic, strong) UIView *leftWarp;
@end

@implementation HEAddressInputCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        _tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectMapClick)];

        _leftWarp = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 49, 24)];
        UIImageView *leftIcon = [[UIImageView alloc] initWithImage:IMAGE_NAMED(@"select_location")];
        leftIcon.contentMode = UIViewContentModeScaleAspectFill;
        leftIcon.frame = CGRectMake(20, 0, 23, 24);
        [_leftWarp addSubview:leftIcon];
        
        [self initUI];
    }
    return self;
}

- (void)initUI {
    
    _titleLb = [UILabel new];
    _titleLb.font = DYFont(15);
    _titleLb.text = @"所在地区";
    _titleLb.textColor = CNavLightBlackColor;
    [self.contentView addSubview:_titleLb];
    
    [_titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView);
        make.left.equalTo(self.contentView).mas_offset(15);
        make.width.mas_offset([_titleLb.text widthForFont:DYFont(15)] + 10);
    }];
    
    _contentTf = [[QMUITextField alloc] init];
    _contentTf.maximumTextLength = 500;
    _contentTf.placeholder = @"请输入内容";
    _contentTf.font = UIFontMake(15);
    _contentTf.layer.cornerRadius = 3;
    _contentTf.textInsets = UIEdgeInsetsMake(0, 10, 0, 10);
    _contentTf.clearButtonMode = UITextFieldViewModeNever;
    _contentTf.delegate = self;
    [self.contentView addSubview: _contentTf];
    _contentTf.layer.borderWidth = 1;
    _contentTf.layer.borderColor = CSplitLineColor.CGColor;
    _contentTf.userInteractionEnabled = YES;
    
    [_contentTf mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).mas_offset(10);
        make.left.equalTo(_titleLb.mas_right).mas_offset(5);
        make.right.equalTo(self.contentView).mas_offset(-10);
        make.height.mas_offset(36);
        make.bottom.equalTo(self.contentView).mas_offset(-10);
    }];
}

- (void)selectMapClick {
    if (_row == 2) {
        if ([_delegate respondsToSelector:@selector(selectAddressWithMap:)]) {
            [_delegate selectAddressWithMap:_model];
        }
    }
}

- (void)refreshContent:(GDFiterCellModel *)fiterModel formModel:(GDFormModel *)formModel indexPath:(NSIndexPath *)indexPath {
    self.delegate = formModel.proxyVC;
    _model = formModel.addressDetails;
    _row = indexPath.row;
    _titleLb.text = fiterModel.leftTitle;
    _contentTf.rightViewMode = UITextFieldViewModeNever;
    [_contentTf removeGestureRecognizer:_tap];
    _contentTf.keyboardType = UIKeyboardTypeDefault;

    if (_row == 0) {
        _contentTf.text = formModel.addressDetails.name;
    }
    else if (_row == 1) {
        _contentTf.keyboardType = UIKeyboardTypePhonePad;
        _contentTf.text = formModel.addressDetails.phoneNumber;
    }
    else if (_row == 2) {
        _contentTf.text = formModel.addressDetails.KTitleString;
        [_contentTf addGestureRecognizer:_tap];

        _contentTf.rightViewMode = UITextFieldViewModeAlways;
        _contentTf.rightView = _leftWarp;
    }
    else if (_row == 3) {
        _contentTf.text = formModel.addressDetails.detailAddress;
    }
}

#pragma mark - TextFiledDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

    NSString *text = [textField.text stringByReplacingCharactersInRange:range withString:string];
    if (_row == 0) {
        self.formModel.addressDetails.name = text;
    }
    else if (_row == 1) {
        self.formModel.addressDetails.phoneNumber = text;
    }
    else if (_row == 2) {

    }
    else if (_row == 3) {
        self.formModel.addressDetails.detailAddress = text;
    }
    return YES;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
